package cmpe226.spring2011.project1;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import cmpe226.spring2011.project1.model.DifferenceDTO;
import cmpe226.spring2011.project1.model.Symbol;
import cmpe226.spring2011.project1.util.HibernateUtil;

public class HibDAO implements DAO {

	
	public static void main(String... args) throws SQLException, ParseException {
		HibDAO dao = new HibDAO();
		
		List<String> symbols = new ArrayList<String>();
		symbols.add("AA");
		symbols.add("AB");
		List<DifferenceDTO> d = dao.getSymbolDifferences("2011-03-01", "2011-03-04", symbols);
		
		for(DifferenceDTO dd : d) {
			System.out.println(dd.getDifference() + dd.getDate());
		}
	}
	
	public List<DifferenceDTO> getSymbolDifferences(String from, String to, List<String> symbols) throws SQLException, ParseException {
		List<DifferenceDTO> r = new ArrayList<DifferenceDTO>();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			Query query = session.createQuery("select new cmpe226.spring2011.project1.model.DifferenceDTO(quote.date, (quote.close - quote.open)/quote.open, symbol.symbol) " +
					"from Symbol as symbol inner join symbol.quotes as quote where symbol.symbol in (:symbols) and quote.date between :from and :to order by symbol.symbol, quote.date asc");
			query.setParameterList("symbols", symbols);
			query.setDate("to", df.parse(to));
			query.setDate("from", df.parse(from));
			r  = query.list();
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			session.getTransaction().rollback();
			throw new SQLException(e);
		}
		return r;
	}
	
	@Override
	public void save(List<Symbol> symlist) throws SQLException {
		if(symlist == null) {
			throw new RuntimeException("Symbol object is null");
		}
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			for(Symbol symbol : symlist) {
			session.save(symbol);
		}
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			session.getTransaction().rollback();
			throw new SQLException(e);
		}
	}

	@Override
	public void save(Symbol symbol) throws SQLException {
		if(symbol == null) {
			throw new RuntimeException("Symbol object is null");
		}
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		try {
			session.beginTransaction();
			session.save(symbol);
			session.getTransaction().commit();
		} catch (RuntimeException e) {
			session.getTransaction().rollback();
			throw new SQLException(e);
		}
	}

	@Override
	public Symbol read(String symbol) throws SQLException {
		if(symbol.equals("") || symbol == null) {
			return null;
		}
		Symbol s = null;
		Session session = HibernateUtil.getSessionFactory().getCurrentSession();
		
		try {
			session.beginTransaction();
			s = (Symbol) session.load(Symbol.class, symbol);
			session.getTransaction().commit();
		} catch(RuntimeException e) {
			session.getTransaction().rollback();
			throw new SQLException(e);
		}
		return s;
	}
}
